#define _CRT_SECURE_NO_WARNINGS
#include"List.h"

ListNode* ListInit()
{
	ListNode* phead = malloc(sizeof(DATE_TYPE));
	phead->date = 0;
	phead->next = phead;
	phead->prev = phead;
	return phead;
}

ListNode* List_buy_newnode(DATE_TYPE x)
{
	ListNode* newnode = malloc(sizeof(ListNode));
	newnode->date = x;
	newnode->next = NULL;
	newnode->prev = NULL;
	return newnode;
}

void List_push_back(ListNode* phead, DATE_TYPE x)
{
	ListNode* newnode = List_buy_newnode(x);
	phead->prev->next = newnode;
	newnode->prev = phead->prev;
	phead->prev = newnode;
	newnode->next = phead;
}

void List_print(ListNode* phead)
{
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		printf("%d->", cur->date);
		cur = cur->next;
	}
	printf("NULL\n");
}

void List_push_head(ListNode* phead, DATE_TYPE x)
{
	ListNode* newnode = List_buy_newnode(x);
	newnode->next = phead->next;
	phead->next->prev = newnode;
	phead->next = newnode;
	newnode->prev = phead;
}

void List_pop_head(ListNode* phead)
{
	ListNode* second = phead->next->next;
	free(phead->next);
	phead->next = second;
	second->prev = phead;
}

void List_pop_back(ListNode* phead)
{
	ListNode* second = phead->prev->prev;
	free(phead->prev);
	phead->prev = second;
	second->next = phead;
}

ListNode* List_find(ListNode* phead, DATE_TYPE x)
{
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->date == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void List_insert(ListNode* phead, ListNode* pop, DATE_TYPE x)
{
	ListNode* newnode = List_buy_newnode(x);
	pop->prev->next = newnode;
	newnode->prev = pop->prev;
	newnode->next = pop;
	pop->prev = newnode;
}

void List_erase(ListNode* phead, ListNode* pop)
{
	pop->prev->next = pop->next;
	pop->next->prev = pop->prev;
	free(pop);
	pop = NULL;
}

void List_destory(ListNode* phead)
{
	ListNode* cur = phead->next;
	while (cur != phead)
	{
		ListNode* tmp = cur;
		free(cur);
		cur = tmp->next;
	}
	free(phead);
	phead = NULL;
}